package net.homeip.jcperera.nu.model.entity;

import javax.inject.Named;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import net.homeip.jcperera.nu.core.msg.BaseMessage;

/**
 * Manages Types for the whole Framework
 * 
 * @name Type Base class
 * @author julio.c.perera@gmail.com
 * @application nu
 * @since 0.1.0.0
 */

@Named(Type.componentName)
@Entity
@Table(name = Type.tableName)
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorValue(value = Type.tableName)
public class Type extends PersistentEntity
{
    // Required to implement Serializable interface
    private static final long  serialVersionUID = -7466789095344068394L;

    // CoreBase properties, for easy use from other components, as well as logging
    public static final int    runtimeLevel     = 1;
    public static final String moduleName       = "model";
    public static final String componentName    = "nu_model_entity_type";

    // All persistent entities should specify a table name
    public static final String tableName        = "TYPE";

    @Basic
    @NotNull
    @Column(name = "CLASS_NAME")
    private String             className;

    // A default constructor is a must for this framework!!
    public Type()
    {
        super();
    }

    /**
     * Getter for property className
     * 
     * @return the className associated to this Type
     */

    public String getClassName()
    {
        return this.className;
    }

    /**
     * <p>
     * Builds an instance of the class associated to this type using the default constructor.
     * </p>
     * <p>
     * <b>Note:</b> The instance should have a public default constructor (a constructor with no parameters) or a error
     * message is returned
     * </p>
     * 
     * @see Class#forName(String)
     * @see Class#newInstance()
     * @return a newly allocated instance of the class represented by this object
     */
    @Transient
    public Object getInstance() throws BaseMessage
    {
        return TypeManager.getInstanceByClassName(this.className);
    }

    /**
     * @param newVal
     */
    public void setClassName(String newVal)
    {
        this.className = newVal;
    }

}